package com.plum.admin.modules.social.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plum.admin.common.util.R;
import com.plum.admin.common.util.SecurityUtil;
import com.plum.admin.modules.social.entity.SysSocial;
import com.plum.admin.modules.social.service.ISysSocialService;
import com.plum.admin.modules.sys.entity.SysUser;
import com.plum.admin.modules.sys.service.ISysUserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/9/16 9:49
 */
@RestController
@RequestMapping("/social/relate")
public class RelateController {

    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysSocialService sysSocialService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping
    public R<Object> relationUser(
            @RequestParam Boolean isLogin,
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("socialType")Integer socialType,
            @RequestParam("id") String id){
        SysUser user=null;
        if(isLogin){
            user = securityUtil.getUser();
            username=user.getUsername();
        }else{
            if(StrUtil.isBlank(username)||StrUtil.isBlank(password)){
                return R.error("用户名或密码不能为空");
            }
            user = userService.findByUsername(username);
            if(ObjectUtil.isNull(user)){
                return R.error("用户"+username+"不存在");
            }
            if(!new BCryptPasswordEncoder().matches(password,user.getPassword())){
                return R.error("密码不正确");
            }
        }
        String key = redisTemplate.opsForValue().get(id);
        if(StrUtil.isBlank(key)){
            return R.error("id错误");
        }
        String socialName = getSocialName(socialType);
        SysSocial sysSocial = sysSocialService.getOne(Wrappers.<SysSocial>query().lambda().eq(SysSocial::getUserName, username).eq(SysSocial::getPlatform, socialType));
        if(ObjectUtil.isNotNull(sysSocial)){
            return R.error("该账户已绑定有"+socialName+"账号,请先进行解绑");
        }
        SysSocial social = sysSocialService.getById(Long.valueOf(key));
        if(ObjectUtil.isNull(social)){
            return R.error("绑定失败，请先进行第三方认证");
        }
        if(StrUtil.isNotBlank(social.getUserName())){
            return R.error(socialName+"已绑定此账号,请先进行解绑");
        }
        social.setUserName(username);
        social.setUserId(user.getId());
        sysSocialService.updateById(social);
        if (!isLogin) {
            String token = securityUtil.getToken(username);
            String jwtKey= IdUtil.simpleUUID();
            redisTemplate.opsForValue().set(jwtKey,token,2L, TimeUnit.MINUTES);
            return R.ok(jwtKey);
        } else {
            return R.ok("绑定成功");
        }
    }
    private String getSocialName(Integer socialType){
        String socialName= Strings.EMPTY;
        switch (socialType) {
            case 1:
                socialName = "支付宝";
                break;
            case 2:
                socialName="微信";
                break;
            case 3:
                socialName="github";
                break;
            case 4:
                socialName="码云";
                break;
            default:
        }
        return socialName;
    }
    @GetMapping("/getToken")
    public R<Object> getToken(String jwtKey){
        String token = redisTemplate.opsForValue().get(jwtKey);
        return R.ok(token);
    }
}
